home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_01 / ed_157 / rec_insert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-10  |  3.2 KB  |  129 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #include "memory.h"
  24. #include "rec.h"
  25. #include "window.h"
  26. #include "ed_dec.h"
  27.  
  28. /******************************************************************************\
  29. |Routine: rec_insert
  30. |Callby: insert
  31. |Purpose: Handles insertion into a record.
  32. |Arguments:
  33. |    prec is the record containing the insertion point. A new prec is returned.
  34. |    byt is the position of the insertion point in that record.
  35. |    length is the length of the data to insert.
  36. |    data is the data to insert.
  37. \******************************************************************************/
  38. void rec_insert(prec,byt,length,data)
  39. rec_ptr *prec;
  40. Int byt,length;
  41. Char *data;
  42. {
  43.     register Int l;
  44.     register rec_ptr rec,new,pred;
  45.     register Char *from,*to;
  46.  
  47.     if(!length)
  48.         return;
  49.     rec = *prec;
  50.     new = (rec_ptr)imalloc(sizeof(rec_node));
  51.     l = rec->length + length;
  52.     new->data = (Char *)imalloc(l + 1);
  53.     new->recflags = 1;    /* it is a freeable buffer */
  54.     from = rec->data;
  55.     to = new->data;
  56.     if(byt > 0)
  57.     {
  58.         memcpy(to,from,byt);
  59.         to += byt;
  60.         from += byt;
  61.     }
  62.     memcpy(to,data,length);
  63.     to += length;
  64.     if(byt < rec->length)
  65.         memcpy(to,from,rec->length - byt);
  66.     new->data[l] = 0;
  67.     new->length = l;
  68.     pred = rec->prev;
  69.     remq(rec);
  70.     if(rec->recflags & 1)
  71.         ifree(rec->data);
  72.     ifree(rec);
  73.     fixup_recs(rec,new);
  74.     for(l = 0;l < NMARK;l++)
  75.         if(MARKREC[l] == rec)
  76.         {
  77.             MARKREC[l] = new;
  78.             if(byt < MARKBYT[l])
  79.                 MARKBYT[l] += length;
  80.         }
  81.     if(rec == SELREC)
  82.     {
  83.         SELREC = new;
  84.         if(byt < SELBYT)
  85.             SELBYT += length;
  86.     }
  87.     insq(new,pred);
  88.     *prec = new;
  89. }
  90.  
  91. /******************************************************************************\
  92. |Routine: rec_overstrike
  93. |Callby: insert
  94. |Purpose: Handles overstrike modification of a record.
  95. |Arguments:
  96. |    rec is the record containing the insertion point.
  97. |    byt is the starting position of the overlay in that record.
  98. |    length is the length of the data to overlay.
  99. |    data is the data to overlay.
  100. \******************************************************************************/
  101. void rec_overstrike(prec,byt,length,data)
  102. rec_ptr *prec;
  103. Int byt,length;
  104. Char *data;
  105. {
  106.     rec_ptr rec;
  107.     Int i;
  108.     Char *p;
  109.     
  110.     rec = *prec;
  111.     if(byt == rec->length)
  112.         rec_insert(prec,byt,length,data);
  113.     else
  114.     {
  115.         if((i = byt + length) > rec->length)
  116.         {
  117.             p = (Char *)imalloc(++i);
  118.             p[--i] = '\0';
  119.             memcpy(p,rec->data,rec->length);
  120.             if(rec->recflags & 1)
  121.                 ifree(rec->data);
  122.             rec->data = p;
  123.             rec->length = i;
  124.         }
  125.         memcpy(rec->data + byt,data,length);
  126.     }
  127. }
  128.  
  129.